var move_obj;
var moves_top;
var _top=0;
var _bottom;
var flag_loaded=false;
var loaded_timer=null;

$(function(){
     var StartX;
     var StartY;
     var MoveX;
     var EndX;
     var mubiaoleft;
     var timerCount = 0;


  var h1 = getStyle( getByClass(document,"shaixuan-title")[0],"height");
  var h2 = getStyle( getByClass(document,"shaixuan-title")[0],"padding-top");
  var h3 = getStyle( getByClass(document,"shaixuan-title")[0],"padding-bottom");
  var h4 = getStyle( getByClass(document,"shaixuan-kuangjia")[0],"margin-top");
  
  var targetheight = window.innerHeight - parseInt(h1)-parseInt(h2)-parseInt(h3)-parseInt(h4);
   document.getElementById("ist-mask").style.height = targetheight+"px";
  getByClass(document.getElementsByTagName("body")[0],"shaixuan-kuangjia")[0].style.height = parseInt(h1)+parseInt(h2)+parseInt(h3)+targetheight+"px";

  var mask_height = getStyle(document.getElementById("ist-mask"),"height");
     
   move_obj = document.getElementById("ist");
   var ist_height =getStyle(move_obj,"height");
   _bottom = parseInt(mask_height) -parseInt(ist_height);


	$("#sousuo").css("left",window.innerWidth+"px");
var mubiao = document.getElementById("screening");

loaded_timer = setInterval(function(){
   if(flag_loaded==true)
   {
        document.getElementById("sousuo").onclick=function(){
        creatSearchMask();
        mubiaoleft = parseInt( (0.18) * (window.innerWidth));
        startMove(mubiao,"left",mubiaoleft);
        document.getElementById("Searchmask").onclick=function(){
           var mubiaoright = parseInt( (1) * (window.innerWidth));
           startMove(mubiao,"left",mubiaoright,removeSearchMask);
        };
   };
    clearInterval(loaded_timer);
   }
},1);  


      
   document.getElementById("canc").onclick=function(){
     var mubiaoright = parseInt( (1) * (window.innerWidth));
     startMove(mubiao,"left",mubiaoright,removeSearchMask);
   };
    
   document.getElementById("conf").onclick=function(){
       removeSearchMask();
       document.getElementById("screening").style.left= parseInt( (1) * (window.innerWidth))+"px";
   }

   try{
       bindToScreening();
   }
   catch (e)
   {
       //alert("拖拽事件不支持");
   }
    
   function bindToScreening(){
   	   document.getElementById("screening").addEventListener("touchstart",touchstartfunC,false);
   	   document.getElementById("screening").addEventListener("touchmove",touchmovefunC,false);
   	   document.getElementById("screening").addEventListener("touchend",touchendfunC,false);
   } 
   
   function touchstartfunC(e) {

      try {
      	  StartX= Number(e.touches[0].pageX);   //记录按下时的横坐标
      	  StartY= Number(e.touches[0].pageY);
      	 
      }
      catch(e){
          
      }
      
   }
   
   function touchmovefunC(e) {
   
  try {
e.preventDefault();
  	     timerCount++;
         MoveX = Number(e.touches[0].pageX);   //记录移动时的横坐标
         //console.log("移动的:"+x);
         //console.log("开始记下的点坐标:"+StartX);
         if(MoveX>StartX) 
         {

            mubiao.style.left = ( mubiaoleft + (MoveX-StartX) ) + "px";
            
         }
      }
      catch(e){

      }
   }

   function touchendfunC(e) {
   try {
          //var endX = Number(e.touches[0].pageX); //记录方开时的横坐标
          console.log(timerCount);
          EndX = MoveX;
          if(EndX > StartX)
          {
          	var speed = Number((EndX-StartX)/timerCount);
          	var ladio = Number((EndX-StartX)/window.innerWidth);
          	 console.log("放开的速度为:"+speed);
          	 console.log("放开的X坐标为:"+EndX);
          	 console.log("屏幕的宽度为:"+window.innerWidth);
          	 console.log("比率为:"+ladio);
          
   	   	 
          if(speed < 60 ) 
          	{
                if(ladio<0.3)
                {
          		mubiaoleft = parseInt( (0.18) * (window.innerWidth));
          		startMove(mubiao,"left",mubiaoleft);
          		}
          		else
          		{
          	    mubiaoleft = parseInt( (1) * (window.innerWidth));
          	    startMove(mubiao,"left",mubiaoleft,removeSearchMask);
          		} 
          	}
          else 
          {   
          	 if(timerCount!=0)
          	 {
          	  mubiaoleft = parseInt( (1) * (window.innerWidth));
          	  startMove(mubiao,"left",mubiaoleft,removeSearchMask);
          	 } 
          }
      }
         	timerCount=0;
      }
      catch(e){

      }
   }

     $("#ist").bind("touchstart",touchchose_startfunC);
     $("#ist").bind("touchmove",touchchose_movefunC);
     $("#ist").bind("touchend",touchchose_endfunC);

});


function creatSearchMask(){
var Searchmask = document.createElement("div"); 
Searchmask.id = "Searchmask"; 
Searchmask.style.position = "fixed"; 
Searchmask.style.top = "0%"; 
Searchmask.style.left = "0"; 
Searchmask.style.zIndex = 10000; 
Searchmask.style.backgroundColor = "#000000"; 
Searchmask.style.filter = "alpha(opacity=45)"; 
Searchmask.style.opacity = "0.45"; 
Searchmask.style.width = "100%"; 
Searchmask.style.height = (window.parent.document.body.scrollHeight + 50) + "px"; 
window.parent.document.body.appendChild(Searchmask);
document.body.onmousewheel = function(){return false;}
document.body.style.overflow="hidden"; 
document.body.scrollTop = 0;
//document.getElementsByTagName("body")[0].removeEventListener('touchmove', touchMoveFunc, false);   
document.body.ontouchmove=function(){
  return false;
}

}

function removeSearchMask()
{
   document.body.removeChild(document.getElementById("Searchmask"));
   document.body.onmousewheel = function(){return true;}
   document.body.style.overflow="scroll";
document.body.ontouchmove=function(){
  return true;
}   
//document.getElementsByTagName("body")[0].addEventListener('touchmove', touchMoveFunc, false);  
}

var chose_StartX;
var chose_StartY;
var chose_StartY2;
var chose_MoveY;
var chose_flag_direction=true;
var chose_timerCount=0;
var chose_EndY;


   function touchchose_startfunC(e) {

      try {
        chose_timerCount = 0;
        moves_top=Number(move_obj.offsetTop);
          chose_StartX= Number(e.touches[0].pageX);   //记录按下时的横坐标
          chose_StartY= Number(e.touches[0].pageY);
          chose_StartY2=chose_StartY;
      }
      catch(e){

      }
      
   }
   
   function touchchose_movefunC(e) {


  try {
        e.preventDefault();
        
        if(chose_MoveY!=null)
         {
           if(Number(e.touches[0].pageY)>chose_MoveY)
           {
              if(chose_flag_direction==false)
                  {
                   chose_timerCount=0;
                   chose_StartY2=Number(e.touches[0].pageY);
                   }
                   else  chose_timerCount++;  
                 chose_flag_direction = true;
           }
           else
           {
              if(chose_flag_direction==true)
                  {
                  chose_timerCount=0;
                  chose_StartY2=chose_MoveY;
                   }
                  else  chose_timerCount++; 

                  chose_flag_direction=false;
           }

         }


        chose_MoveY = Number(e.touches[0].pageY);   //记录移动时的横坐标
        
        if((moves_top + (chose_MoveY-chose_StartY))<=_top && (moves_top + (chose_MoveY-chose_StartY))>=_bottom)   
        {
          move_obj.style.top=moves_top + (chose_MoveY-chose_StartY)+"px";
        }
        
      }
      catch(e){
      }
   }

   function touchchose_endfunC(e) {
   try {
          moves_top=Number(move_obj.offsetTop);
          chose_EndY = chose_MoveY;
          // console.log("chose_StartY2"+chose_StartY2);
          // console.log("chose_EndY"+chose_EndY);
          // console.log("moves_top"+moves_top);

            if(chose_EndY > chose_StartY2 && moves_top<_top)
              {
                var speed = Number((chose_EndY-chose_StartY2)/chose_timerCount);
                 //console.log("放开的正速度为:"+speed);
                 //console.log("放开的Y坐标为:"+chose_EndY);
                 startMoveMy(move_obj,"top",moves_top+parseInt(speed*3));
              }
              else if(chose_EndY <= chose_StartY2 && moves_top>_bottom)
              {
                 var speed = Number((chose_StartY2-chose_EndY)/chose_timerCount);

                 //console.log("放开的负速度为:"+speed);
                 //console.log("放开的Y坐标为:"+EndY);
                 startMoveMy(move_obj,"top",moves_top-parseInt(speed*3));
              }
  

      }
      catch(e){

      }
   }


/*运动框架，包括链式运动框架,iTarget不带单位*/
function startMoveMy(obj, attr, iTarget, fn)
{

  clearInterval(obj.timer);
  obj.timer=setInterval(function (){
    //1.取当前的值
    var iCur=0;
    
    if(attr=='opacity')
    {
      iCur=parseInt(parseFloat(getStyle(obj, attr))*100);
    }
    else
    {
      iCur=parseInt(getStyle(obj, attr));
    }
    
    //2.算速度
    var iSpeed = (iTarget-iCur)/5;
    iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
    
    //3.检测停止
    if(iCur==iTarget)
    {  
      clearInterval(obj.timer);
      if(fn)
      {
        fn();
      }
    }
    else
    {
      if(attr=="top"&&iCur>_top&&iTarget>iCur)
      {
         clearInterval(obj.timer);
         obj.style[attr]=_top+'px';

      }
      else if(attr=="top"&&iCur<=_bottom&&iTarget<iCur)
      {
         clearInterval(obj.timer);
         obj.style[attr]=_bottom+'px';
      }
      else
      {
        if(attr=='opacity')
        {
          obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';
          obj.style.opacity=(iCur+iSpeed)/100;
        }
        else
        {
          obj.style[attr]=iCur+iSpeed+'px';
        }
      } 

    }
  }, 30)
}     